package com.tg.app.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;
import com.tg.app.activity.device.settings.AlertSoundListActivity;
import com.tange.base.toolkit.StringUtils;
import com.tg.data.http.entity.DeviceBellBean;
import com.tg.appcommon.android.ResourcesUtil;

import java.util.List;

/**
 * Created by rance on 2020-02-23 21:40
 */
public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.AlarmViewHolder> {
    public  static  final  String  ALARM_DEFAULT = "0";
    public static final int LIST_FIRST = 0;
    public static final int LIST_MID = 1;
    public static final int LIST_TAIL = 2;
    public static final int LIST_ONLY = 3;

    private List<DeviceBellBean> deviceBellBeanList;
    private  OnAlarmClickListener onAlarmClickListener;
    private String  checkID;
    private String from;

    public void setDeviceBellBeanList(List<DeviceBellBean> deviceBellBeanList) {
        this.deviceBellBeanList = deviceBellBeanList;
    }

    public AlarmAdapter(List<DeviceBellBean> list) {
        deviceBellBeanList = list;
    }

    public AlarmAdapter(List<DeviceBellBean> list, String from) {
        deviceBellBeanList = list;
        this.from = from;
    }

    public void setOnAlarmClickListener(OnAlarmClickListener onAlarmClickListener) {
        this.onAlarmClickListener = onAlarmClickListener;
    }

    public void setCheckID(String checkID) {
        this.checkID = checkID;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (deviceBellBeanList != null) {
            if (deviceBellBeanList.size() == 1){
                return LIST_ONLY;
            }
            if (position == LIST_FIRST){
                return LIST_FIRST;
            }
            if (position == deviceBellBeanList.size() - 1){
                return LIST_TAIL;
            }
        }
        return LIST_MID;
    }


    @NonNull
    @Override
    public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_alarm_item, parent, false);
        return new AlarmViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AlarmViewHolder holder, final int position) {

        DeviceBellBean bellBean = deviceBellBeanList.get(position);
        holder.tvTitle.setText(bellBean.getDescription());
        holder.ivPlay.setVisibility(View.VISIBLE);
        holder.ivPlay.setImageResource(R.drawable.ic_tange_alarm_play);
        holder.btnCustom.setVisibility(View.VISIBLE);

        holder.btnCustom.setOnClickListener(v -> {
            if (onAlarmClickListener!=null){
                onAlarmClickListener.onItemDel(position);
            }
        });
        int id = Integer.parseInt(bellBean.getId());
        if (id >= 0){
            holder.btnCustom.setVisibility(View.GONE);
        }else if (bellBean.getDescription().equals(ResourcesUtil.getString(R.string.txt_current_voice))){
            holder.btnCustom.setVisibility(View.GONE);
        }

        if (!StringUtils.equals(AlertSoundListActivity.EXT_SOUND_LIST_FROM_LAMP, from)){
            if (!TextUtils.isEmpty(checkID)){
                if (TextUtils.equals(checkID,bellBean.getId())) {
                    holder.ivCheck.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_yes);
                    holder.btnCustom.setVisibility(View.GONE);
                }else {
                    holder.ivCheck.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_no);
                }

            }else {
                holder.btnCustom.setVisibility(View.GONE);
                if (TextUtils.equals(ALARM_DEFAULT,bellBean.getId())){
                    holder.ivCheck.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_yes);
                }else {
                    holder.ivCheck.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_no);
                }
            }
        }

        holder.ivPlay.setOnClickListener(view -> {
            if (onAlarmClickListener!=null){
                onAlarmClickListener.onItemPlayClick(position);
            }
        });
        holder.itemView.setOnClickListener(view -> {
            if (onAlarmClickListener!=null){
                onAlarmClickListener.onItemCheck(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return deviceBellBeanList == null ? 0 : deviceBellBeanList.size();
    }

    public static class AlarmViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        ImageView ivPlay;
        ImageView ivCheck;
        ImageView  btnCustom;
        public AlarmViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_alarm_item_title);
            ivPlay = itemView.findViewById(R.id.iv_alarm_item_play);
            ivCheck = itemView.findViewById(R.id.iv_alarm_item_check);
            btnCustom = itemView.findViewById(R.id.btn_alert_sound_custom);
        }
    }
    public interface OnAlarmClickListener {
        void onItemPlayClick(int position);
        void onItemCheck(int position);
        void onItemDel(int position);
    }
}
